﻿namespace DevBox
{
    /// <summary>
    /// 分页信息
    /// </summary>
    public class PageInfo
    {
        /// <summary>
        /// 总页数
        /// </summary>
        public int TotalPage { get; set; }
        /// <summary>
        /// 页码
        /// </summary>
        public int PageNo { get; set; }
        /// <summary>
        /// 页数据大小
        /// </summary>
        public int PageSize { get; set; }
        /// <summary>
        /// 总条数
        /// </summary>
        public int Total { get; set; }

        public PageInfo(int pageNo,int pageSize,int total)
        {
            PageNo = pageNo;
            PageSize = pageSize;
            Total = total;

        }
    }

    public class CommResult
    {
        /// <summary>
        /// 返回码     默认0成功
        /// </summary>
        public string Code { get; internal set; } = "0";
        /// <summary>
        /// 返回消息    默认success成功
        /// </summary>
        public string Message { get; internal set; } = "success";
        internal CommResult()
        {

        }

        public static CommResult Success(string code = "", string message = "")
        {
            var result = new CommResult { Code = code, Message = message };
            if (!string.IsNullOrEmpty(code))
            {
                result.Code = code;
            }
            if (!string.IsNullOrEmpty(message))
            {
                result.Message = message;
            }
            return result;
        }
        public static CommResult<T> Success<T>(T data, PageInfo? pageInfo = null, string code = "", string message = "")
        {
            var result = new CommResult<T> { Data = data, PageInfo = pageInfo };
            if (!string.IsNullOrEmpty(code)) 
            {
                result.Code = code;
            }
            if (!string.IsNullOrEmpty(message))
            {
                result.Message = message;
            }
            return result;
        }

        public static CommResult Fail(string code,string message)
        {
            if (string.IsNullOrEmpty(code))
            {
                throw new ArgumentNullException(code);
            }
            if (string.IsNullOrEmpty(message))
            {
                throw new ArgumentNullException(message);
            }
            return new CommResult { Code = code, Message = message };
        }
        public static CommResult<T> Fail<T>(string code, string message)
        {
            if (string.IsNullOrEmpty(code))
            {
                throw new ArgumentNullException(code);
            }
            if (string.IsNullOrEmpty(message))
            {
                throw new ArgumentNullException(message);
            }
            return new CommResult<T> { Code = code, Message = message };
        }
    }

    public class CommResult<TResult>: CommResult
    {

        /// <summary>
        /// 返回结果
        /// </summary>
        public TResult? Data { get; internal set; }
        /// <summary>
        /// 分页信息
        /// </summary>
        public PageInfo? PageInfo { get; internal set; }

        internal CommResult()
        {

        }
    }
}
